package com.chif.goingplus.controller;

import com.chif.goingplus.pojo.*;
import com.chif.goingplus.service.BusService;
import com.chif.goingplus.service.OrderService;
import com.chif.goingplus.utils.JsonUtils;
import com.chif.goingplus.websocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * websocket请求接口   弃用！！！
 */
@RestController
public class WebController {

    @Autowired
    private WebSocket webSocket;

    @GetMapping("/geChartData/socket")
    public String getSocket(String chartId) throws InterruptedException {
        //System.out.println(chartId+"接收到的数据+++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 0; i < 100; i++){
            Thread.sleep(1000);
            webSocket.sendTextMessage(chartId, getchartData(chartId));
        }
        return JsonUtils.getJson( "ok");
    }


    public String getchartData(String chartId){
        String chartData=null;
        switch (chartId){
            case "trend":chartData= getTrendData();break;
            case "rank":chartData= getRankData();break;
            case "hot":chartData= getHotData();break;
            case "stock":chartData= getStockData();break;
            case "map":chartData= getMapData();break;
            case "count":chartData= getOrderAndIncomeCount();break;
        }
        return chartData;
    }



    @Autowired
    private OrderService orderService;

    @Autowired
    private BusService busService;

    public String getTrendData(){
        Trends trends = new Trends();

        //封装客源趋势图
        Trend<PosMonth> orgMonthTrend = orderService.getOrgMonthTrend();
        trends.setOriginTrend(orgMonthTrend);

        //封装去向趋势图
        Trend<PosMonth> disMonthTrend = orderService.getDisMonthTrend();
        trends.setDestinationTrend(disMonthTrend);

        //封装通用数据
        trends.setCommon(new Common());

        //封装下拉框的数据
        List<SelectType> selectTypes = new ArrayList();
        selectTypes.add(new SelectType("originTrend","客源分析趋势"));
        selectTypes.add(new SelectType("destinationTrend","去向分析趋势"));
        trends.setType(selectTypes);

        //返回所有趋势图
        return JsonUtils.getJson(trends);
    }


    public String getRankData(){
        List<RegionOrder> regionOrder = orderService.getRegionOrder();
        return JsonUtils.getJson(regionOrder);
    }

    public String getHotData(){
        List<Hots> hotList = orderService.getHotList();
        return JsonUtils.getJson(hotList);
    }

    public String getStockData(){
        List<Stock> stocks = busService.queryAllLoad();
        return JsonUtils.getJson(stocks);
    }

    public String getMapData(){
        //返回客源坐标信息
        return "[\n" +
                "    {\n" +
                "        \"name\": \"黄金用户\",\n" +
                "        \"children\": [\n" +
                "            {\n" +
                "                \"name\": \"武汉\",\n" +
                "                \"value\": [114.31, 30.52]\n" +
                "            }, {\n" +
                "                \"name\": \"丹东\",\n" +
                "                \"value\": [124.37, 40.13]\n" +
                "            }, {\n" +
                "                \"name\": \"张家口\",\n" +
                "                \"value\": [114.87, 40.82]\n" +
                "            }, {\n" +
                "                \"name\": \"深圳\",\n" +
                "                \"value\": [114.07, 22.62]\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    {\n" +
                "        \"name\": \"白金用户\",\n" +
                "        \"children\": [\n" +
                "            {\n" +
                "                \"name\": \"金华\",\n" +
                "                \"value\": [119.64, 29.12]\n" +
                "            }, {\n" +
                "                \"name\": \"西安\",\n" +
                "                \"value\": [108.95, 34.27]\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    {\n" +
                "        \"name\": \"砖石用户\",\n" +
                "        \"children\": [\n" +
                "            {\n" +
                "                \"name\": \"成都\",\n" +
                "                \"value\": [104.06, 30.67]\n" +
                "            }\n" +
                "        ]\n" +
                "    }\n" +
                "]";
    }

    public String getOrderAndIncomeCount(){
        return JsonUtils.getJson(orderService.getOrderAndIncomeCount());
    }
}
